﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYue.Permission.BLL;
using XinYue.Permission.Mode;
using XinYue.Permission.Web.Models;

namespace XinYue.Permission.Web.Controllers
{
    public class ModuleController : Controller
    {
        /// <summary>
        /// 实例业务逻辑层
        /// </summary>
        private ModuleService baseService = new ModuleService();

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult AddModule(Module module)
        {
            Operate operate = new Operate();
            operate.Success = baseService.Add(module);
            return Json(operate);
        }
        #endregion
        #region 查询名称是否存在
        public JsonResult SelModule(Module module, string name)
        {
            Operate operate = new Operate();
            Expression<Func<Module, bool>> lambdaExpression = a => a.Name == name;
            operate.Success = baseService.Exists(lambdaExpression);
            return Json(operate);
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult UpdateModule(Module module)
        {
            Operate operate = new Operate();
            try
            {
                Expression<Func<Module, bool>> whereladam = a => a.Id == module.Id;
                Module mo=baseService.Query(whereladam);
                mo.Name = module.Name;
                mo.ParentId = module.ParentId;
                mo.SortId = module.SortId;
                mo.Remark = module.Remark;
                operate.Success = baseService.Update(mo);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
            return Json(operate);
        }
        #endregion

        #region 修改状态
        public JsonResult UpdateZt(int id, int isEnable)
        {
            Expression<Func<Module, bool>> lambdaExpression = a => a.Id == id;
            Module module = baseService.Query(lambdaExpression);
            module.IsEnable = isEnable;

            Operate operateResult = new Operate();
            operateResult.Success = baseService.Update(module);
            return Json(operateResult);
        }

        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult DelModule(Module module)
        {
            Operate operate = new Operate();
            operate.Success = baseService.Delete(module);
            return Json(operate);
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteByID(int[] ids)
        {
            Operate operate = new Operate();
            operate.Success = baseService.BatchDeleteByID(ids);
            return Json(operate);
        }
        #endregion

        #region 查询分页
        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult GetModulePageList(int page,int limit)
        {
            PageListResult<Module> pageListResult = new PageListResult<Module>();
            pageListResult.code=0;
            pageListResult.msg = string.Empty;
            int count = 0;
            Expression<Func<Module, bool>> Wherelambda = c => true;
            Expression<Func<Module, int>> OrderBylambda = c => c.Id;
            pageListResult.data = baseService.QueryPageList(page,limit, Wherelambda, OrderBylambda,out count);
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        #endregion
      
        #region 查询右侧栏块--未完成
        public JsonResult GetMenu(int id)
        {
            List<Menu> list = new List<Menu>();
            MenuService menu = new MenuService();
            Expression<Func<Menu, bool>> lambdaExpression = a => a.ModuleId == id;
            list = menu.QueryList(lambdaExpression);
            return Json(list);
        }
        #endregion
    }
}